﻿using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Themes.Resources;
using HomagGroup.Base.UI.Windows;
using Newtonsoft.Json;
using Proxy;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HomagChina.CCHCS
{

    public class Utility
    {
        private SearchInfoHandle Handle_Search;
        public Utility()
        {
            Handle_Search = new SearchInfoHandle();
        }

        public static object GetViewModelInstance(string typeName)
        {
            typeName = "HomagChina.CCHCS.ViewModels." + typeName.Split('.')[3] + "ViewModel";
            var viewModelType = Type.GetType(typeName);
            if (viewModelType != null)
            {
                var viewModelInstance = Activator.CreateInstance(viewModelType);
                return viewModelInstance;
            }
            else
            {
                return null;
            }
        }

        public static UserControl GetUserControlInstance(string typeName)
        {
            typeName = "HomagChina.CCHCS.Views." + typeName;
            var userControlType = Type.GetType(typeName);
            if (userControlType != null)
            {
                var userControlInstance = (UserControl)Activator.CreateInstance(userControlType);
                return userControlInstance;
            }
            else
            {
                return null;
            }
        }

        public static T DeepCopy<T>(T obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }
            var json = JsonConvert.SerializeObject(obj);
            return JsonConvert.DeserializeObject<T>(json);
        }

        public static bool CloseDiaLog(string Title)
        {
            bool result = false;
            try
            {
                foreach (System.Windows.Window window in Application.Current.Windows)
                {
                    if (window.Title.Equals(Title))
                    {
                        window.Close();
                        return result = true;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
        }


        public static void CloseApp()
        {
            try
            {
                if (HomagGroup.Base.UI.Windows.MessageBox.Show(GetLocalText("Close application") + "\r\n", GetLocalText("Prompt message"), MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                {
                    Application.Current.Shutdown();
                }
            }
            catch (Exception ex)
            {

            }
        }

        public void OpenInformationCommand()
        {
            InformationModel Data = Handle_Search.GetSystemInfoMation();

            var version = new Version(Data.Version == null ? "" : Data.Version);
            var Desc = Data.Describe;
            var Title = Data.Title;
            var licenses = new List<string>(Data.licenses.Split(','));
            var appIcon = Application.Current.TryFindResource(InternalKeys.MessageInformationImage) as DrawingBrush;
            var components = new Dictionary<string, string>
            {
                { "powerTouch", Data.powerTouch},
                { "powerControl", Data.powerControl},
                { "HomagGroup.Base.Ui", Data.HomagGroupBaseUi },
                { "Equipment Number",Data.EquipmentNumber},
                { "Main Operating Interface", Data.MainOperatingInterface },
                { "Server Address", Data.ServerAddress },
                { "DataBase Server" ,Data.DataBaseServer },
                { "Database Name", Data.DataBaseName}
            };
            var dialog = new InfoDialog(Desc, Title, version, 1, appIcon, components, licenses);
            dialog.SetValue(UIAdjustments.DisplayModeProperty, DisplayMode.NonTouch);
            dialog.Owner = UIAdjustments.ActiveWindow;
            dialog.ShowDialog();
        }

        //调用基类的GetLocaText方法
        public static string GetLocalText(string text)
        {
            return Proxy.Utility.GetLocalText(text);
        }
    }
}